//
//  UIColor+Extension.swift
//  zhengwutong
//
//  Created by Bernie on 16/8/22.
//  Copyright © 2016年 wangshuguan. All rights reserved.
//

import UIKit

extension UIColor {
    /// 这是以sRGB为基础的取色
    ///
    /// - Parameters:
    ///   - r: 红 255为最大
    ///   - g: 绿 255为最大
    ///   - b: 蓝 255为最大
    ///   - a: 透明度 1为最大
    public convenience init(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat=1){
        self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
    }
    public static func hex(_ hex:String)->UIColor{
        var cString: String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
        if cString.count < 6 {
            return UIColor.black
        }
        if cString.hasPrefix("0X") {
            cString = cString[2..<hex.count]
        }
        if cString.hasPrefix("#") {
            cString = cString[1..<hex.count]
        }
        guard cString.count == 6 else {
            return UIColor.black
        }
        let rString = cString[0..<2]
        let gString = cString[2..<4]
        let bString = cString[4..<6]
        if #available(iOS 13.0, *) {
            var r: UInt64 = 0x0
            var g: UInt64 = 0x0
            var b: UInt64 = 0x0
            Scanner(string: rString).scanHexInt64(&r)
            Scanner(string: gString).scanHexInt64(&g)
            Scanner(string: bString).scanHexInt64(&b)
            return UIColor(CGFloat(r), CGFloat(g), CGFloat(b))
        } else {
            // Fallback on earlier versions
            var r: UInt32 = 0x0
            var g: UInt32 = 0x0
            var b: UInt32 = 0x0
            Scanner(string: rString).scanHexInt32(&r)
            Scanner(string: gString).scanHexInt32(&g)
            Scanner(string: bString).scanHexInt32(&b)
            return UIColor(CGFloat(r), CGFloat(g), CGFloat(b))
        }
    }
    static let danger = UIColor(237,87,60)
    static let unavailable = UIColor(255,152,0)
    static let error = UIColor(255,56,36)
    static let button = UIColor(3,121,251)
    static let systemGreenColor = UIColor(104,218,101)
    static var random:UIColor{
        get{
            return UIColor(CGFloat(arc4random() % 256)/255.0,CGFloat(arc4random() % 256)/255.0,CGFloat(arc4random() % 256)/255.0)
        }
    }
}
